
const apiUrl = 'http://www.apimt.net/api/';
const serverUrl = 'https://www.apimt.net/';

// const apiUrl = 'http://apimt.c1130.cn/api/';
// const serverUrl = 'http://apimt.c1130.cn/';

class request {
	constructor() {
	    
	}
	
	// params 请求参数 retry重试次数
	send(params, retry=0){
		
		
		//如果不说明不需要拼接url，拼接url
		if(params.joinUrl== null) {
			params.joinUrl = true;
		}
		
		if(params.joinUrl) {
			params.url = apiUrl+params.url;
		}
		
				
		//请求方式
		if(!params.type){
			params.type = 'POST';
		}
		
		if(!params.data) {
			params.data = {}
		}
		
		
		
		var token = uni.getStorageSync('token');
		params.data.token = token;
		params.data.mobileType = uni.getSystemInfoSync().platform;
		
		
		
		//如果需要登录的接口却没有登录，跳转到登录页面
		if(params.needLogin && !token){
			this.login();return;
		}
		
		var that = this;
		
		// console.log(params);return;
		uni.request({
			url: params.url, //仅为示例，并非真实接口地址。
			data: params.data,
			method: params.type,
			dataType: 'json',
			header: {
				'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
			},
			success: (res) => {
				if(params.checkCode === false){
					params.sCallback && params.sCallback(res);
				} else if(res.data.code == 401){
					//去登录 
					that.login();return;
				} else if(res.data.code == 400){
					if(params.eCallback) {
						params.eCallback(res);
					} else {
						this.alert(res.data.msg);
					}
				} else {
					params.sCallback && params.sCallback(res.data);
				}
			},
			fail: (res) => {
				console.log('请求出错');
				console.log(res);
				
				
				//如果要求重试
				if(retry > 0) {
					params.joinUrl = false;
					retry = retry -1;
					that.send(params, retry);
				} else if(params.eCallback){
					params.eCallback(res);
				} else {
					uni.showModal({
						title:'错误提示',
						content: '请求出错'
					})
				}
			},
			complete: (res) => {
				
			}
		});
	}
	
	login() {
		// #ifdef MP-WEIXIN
			uni.navigateTo({
				url: '/pages/my/auth'
			})
		// #endif
		
		// #ifndef MP-WEIXIN
			uni.navigateTo({
				url: '/pages/my/login'
			})
		// #endif
	}
	
	alert(msg, callback){
		uni.showModal({
			content:msg,
			showCancel:false,
			success:function(){
				
				callback && callback()
			}
		})
	}
	
	uploadImg(params) {
		uni.uploadFile({
            url: apiUrl+'ajax/upload', //仅为示例，非真实的接口地址
            filePath: params.data,
            name: 'file',
            formData: {
                'user': 'test'
            },
			dataType: 'json',

            success: (res) => {
				var res = JSON.parse(res.data);
				if(res.code == 200 ) {
					params.sCallback && params.sCallback(res);
				} else{
					this.alert('上传出错，请稍候重试');
				}
                
            },
			fail() {
				this.alert('上传出错，请稍候重试');
			}
        });
	}
	
	wxLogin(wxInfo, callback) {
		// #ifdef MP-WEIXIN
		
		
		var that = this;
	    wxInfo.merid = 2;
	    wxInfo.mobileType = 'smallapp';
	    var accountInfo = wx.getAccountInfoSync();
	    wxInfo.appId = accountInfo.miniProgram.appId;
	    console.log(accountInfo);

		//如果用户是通过用户转发过来的，推荐人ID会存储在本地
		wxInfo.recommend_id = uni.getStorageSync('recommendId');
		uni.request({
			url: apiUrl+'weixinauth/getopenid',
			method: 'POST',
			data: wxInfo,
			success: function(res) {
				console.log(res);
				if(res.data.code == 200) {
					var userData = res.data.data;
					uni.setStorageSync('token', userData.userinfo.token);
					uni.setStorageSync('userinfo', userData.userinfo);
					uni.reLaunch({
						url: '/pages/coupon/index'
					})
				} else {
					console.log(res);
					wx.showToast({
						title: res.msg,
						icon: 'none',
					});
				}
			}
		});
		// #endif
	}
}

const Request = new request();
Request.appName = '壹壹荟';
Request.serverUrl = serverUrl;
export default Request 